package com.yijiu.trb;

import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.resource.InvestRecordDetails;
import com.yijiu.response.InvestRecordDetailsResponse;
import com.yijiu.utils.ImageUtils;

/**
 * 投资详情
 * 
 * @author TangChao_Lenovo
 */
public class InvestmentDetailsActivity extends BTitleActivity
{
	private InvestRecordDetails investRecordDetails;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_investment_details_layout);
		requestInvestRecordDetails();
	}

	private void findViewsInit()
	{
		titleView.setText("投资详情");
		TextView moneyTextView = (TextView) findViewById(R.id.tv_investment_details_amount);
		TextView peopleTextView = (TextView) findViewById(R.id.tv_investment_details_name);
		TextView earningsTextView = (TextView) findViewById(R.id.tv_investment_details_earnings);
		TextView projectTextView = (TextView) findViewById(R.id.tv_investment_details_project);
		TextView deadlineTextView = (TextView) findViewById(R.id.tv_investment_details_financing_deadline);
		TextView yearEarningsTextView = (TextView) findViewById(R.id.tv_investment_details_year_earnings);
		TextView dateTextView = (TextView) findViewById(R.id.tv_investment_details_date);
		TextView foundTextView = (TextView) findViewById(R.id.tv_investment_details_found);
		TextView arriveTextView = (TextView) findViewById(R.id.tv_investment_details_arrive);
		TextView streamTextView = (TextView) findViewById(R.id.tv_investment_details_stream);
		/**
		 * 担保机构
		 */
		TextView dbTeamTextView = (TextView) findViewById(R.id.tv_project_details_db_team);
		/**
		 * 担保机构话语
		 */
		TextView dbWordTextView = (TextView) findViewById(R.id.tv_project_details_db_word);
		/**
		 * 担保承诺函标题
		 */
		TextView dbTitle = (TextView) findViewById(R.id.tv_project_details_dbTitle);
		/**
		 * 担保函图片
		 */
		ImageView imageView = (ImageView) findViewById(R.id.iv_project_details_db_pic);
		moneyTextView.setText("投资金额(元)：￥" + investRecordDetails.amount);
		peopleTextView.setText("交易对方：" + investRecordDetails.userName);
		String str = "<font color=\"#545353\">收益金额(元)：￥"
				+ investRecordDetails.divisionAmount
				+ "</font><font color=\"#FF7000\">"
				+ (StringUtils.isNull(investRecordDetails.profitAmount) || Double.valueOf(investRecordDetails.profitAmount) == 0 ? ""
						: "+" + investRecordDetails.profitAmount) + "</font>";
		String str1 = "<font color=\"#545353\">年化收益率：" + investRecordDetails.interestRate + "</font><font color=\"#FF7000\">"
				+ (StringUtils.isNull(investRecordDetails.extInterest) ? "" : "+" + investRecordDetails.extInterest) + "</font>";
		earningsTextView.setText(Html.fromHtml(str));
		projectTextView.setText("投资项目：" + investRecordDetails.tradeName);
		deadlineTextView.setText("融资期限：" + investRecordDetails.timeLimit);
		yearEarningsTextView.setText(Html.fromHtml(str1));
		dateTextView.setText("投资日期：" + investRecordDetails.tradeDate);
		/*---------------------------------------------------------*/
		if (investRecordDetails.effectiveDateTime.equals(""))
		{
			foundTextView.setText("成立日期：" + "待成立");
		}
		else
		{
			foundTextView.setText("成立日期：" + investRecordDetails.effectiveDateTime);
		}
		if (investRecordDetails.expireDateTime.equals(""))
		{
			arriveTextView.setText("到期日期：" + "待成立");
		}
		else
		{
			arriveTextView.setText("到期日期：" + investRecordDetails.expireDateTime);
		}
		/*---------------------------------------------------------*/
		streamTextView.setText("流水单号：" + investRecordDetails.investflowCode);
		dbTeamTextView.setText("担保机构：" + investRecordDetails.guaranteeName);
		dbWordTextView.setText("担保机构话语：" + investRecordDetails.guaranteeStatement);
		String abcString = investRecordDetails.guaranteeAudit;
		if (abcString.equals("IS"))
		{
			dbTitle.setText("担保函：");
		}
		else if (abcString.equals("NO"))
		{
			dbTitle.setText("担保承诺函：");
		}
		ImageUtils.setImage(investRecordDetails.urlPIC, imageView);
	}

	/**
	 * 请求项目列表
	 */
	private void requestInvestRecordDetails()
	{
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=appInvestDetail&tradeId=" + getIntent().getStringExtra("tradeId") + "&detailId="
				+ getIntent().getStringExtra("detailId");
		Log.e("result", inputStr);
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction requestData = new BaseAction(encodedData, encriptData, CacheData.APP_KEY);
		// 登录响应
		InvestRecordDetailsResponse investRecordDetailsResponse = new InvestRecordDetailsResponse();
		// 请求服务器
		Request request = new Request(requestData, investRecordDetailsResponse, ActionType.INVEST_RECORD_DETAILS);
		netManager.excute(request, this, this);
		showProgress("正在获取投资详情，请稍候");
	}

	/* 操作成功返回的函数 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		int type = r.getType();
		switch (type)
		{
			case ActionType.INVEST_RECORD_DETAILS:
				InvestRecordDetailsResponse investRecordDetailsResponse = (InvestRecordDetailsResponse) r.getResponse();
				investRecordDetails = investRecordDetailsResponse.investRecordDetails;
				findViewsInit();
				break;
		}
	}

	/* 操作失败返回的函数 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.INVEST_RECORD_DETAILS:
				app.showMsg(errorMsg);
				break;
		}
	}

	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
}
